package com.ericsson.enap.modules.monitor.service.impl;

import com.ericsson.enap.modules.topology.entity.CabSwitch;
import com.ericsson.enap.modules.monitor.entity.SwitchInterfaceStatus;
import com.ericsson.enap.modules.monitor.entity.SwitchPortData;
import com.ericsson.enap.modules.monitor.entity.SwitchTotalData;
import com.ericsson.enap.modules.monitor.service.SwitchEquipDataService;
import com.ericsson.common.util.TelnetUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Service("hw")
public class SwitchDataServiceHWImpl implements SwitchEquipDataService {
    @Override
    public String getAlarm(TelnetUtils telnetUtils, String command) {
        String result = telnetUtils.sendCommand(command);
        return result;
    }

    @Override
    public SwitchTotalData getUpTime(TelnetUtils telnetUtils, String command) {
        SwitchTotalData switchTotalData = new SwitchTotalData();
        String result = telnetUtils.sendCommand(command);
        String[] s = result.split("\n");
        switchTotalData.setVersion(s[2].replace("VRP (R) software, Version ", ""));
        switchTotalData.setUptime(s[4].substring(s[4].indexOf("uptime is") + 10).replace(" weeks,", "w").replace(" days,", "d").replace(" hours,", "h").replace(" minutes", "m"));
        return switchTotalData;
    }

    @Override
    public String getVlan(TelnetUtils telnetUtils, String command) {
        String result=telnetUtils.sendCommandVlan(command);
        return result;
    }

    @Override
    public void displayPage(TelnetUtils telnetUtils, String command) {
        telnetUtils.sendCommand(command);
    }

    @Override
    public String getIpv4Route(TelnetUtils telnetUtils, String command) {
        String result=telnetUtils.sendCommand(command);
        return result;
    }

    @Override
    public String getIpv6Route(TelnetUtils telnetUtils, String command) {
        String result=telnetUtils.sendCommand(command);
        return result;
    }

    @Override
    public String getConfiguration(TelnetUtils telnetUtils, String command) {
        String result=telnetUtils.sendCommand(command,2);
        return result;
    }

    @Override
    public String getInterface(TelnetUtils telnetUtils, String command) {
        String result = telnetUtils.sendCommand(command);
        return result;
    }

    @Override
    public List<SwitchInterfaceStatus> explainInterfaceStatus(String interfaceMsg, String switchName, Map<String, String> statusMap) {
        List<SwitchInterfaceStatus> switchInterfaceStatusList = new ArrayList<>();
        SwitchInterfaceStatus switchInterfaceStatus;
        String[] temp = interfaceMsg.split("\n");
        int remark = 0;
        for (String s : temp) {
            if (remark != 0) {
                String[] sta = s.split(" ");
                if (sta.length > 5&&(sta[0].startsWith("10GE")||sta[0].startsWith("40GE"))) {
                    switchInterfaceStatus = new SwitchInterfaceStatus();
                    switchInterfaceStatus.setSwitchName(switchName);
                    String phy="";
                    String pro="";
                    int j = 0;
                    for (String m : sta) {
                        if (!m.equals("")) {
                            if (j < 5) {
                                switch (j) {
                                    case 0:
                                        switchInterfaceStatusList.add(switchInterfaceStatus);
                                        switchInterfaceStatus.setOriginalInterface(m);
                                        switchInterfaceStatus.setSwitchInterface(m);
                                        break;
                                    case 1:
                                        phy=m.toUpperCase();
                                        break;
                                    case 2:
                                        pro=m.toUpperCase();
                                        String status;
                                        if(phy.equals("UP")&&pro.equals("UP")){
                                            status="UP";
                                        }else if(phy.equals("DOWN")){
                                            status="DOWN";
                                        }else{
                                            status="ADM";
                                        }
                                        if (null!=statusMap&&null!=statusMap.get(status)&&!"".equals(statusMap.get(status))) {
                                            switchInterfaceStatus.setSwitchStatus(Long.parseLong(statusMap.get(status)));
                                        } else {
                                            switchInterfaceStatus.setSwitchStatus(-1);
                                        }
                                        break;
                                }
                            } else {
                                break;
                            }
                            j++;
                        }
                    }
                }
            }
            if (s.contains("Interface") && s.contains("PHY") && s.contains("Protocol") && s.contains("InUti") && s.contains("OutUti") && s.contains("inErrors")) {
                remark = 1;
            }
        }
        return switchInterfaceStatusList;
    }

    @Override
    public List<SwitchPortData> getInterfaceDetails(TelnetUtils telnetUtils, String command, String programDate, CabSwitch cabSwitch, Map<String, SwitchPortData> beforeSwitchPortDataMap) {
        List<SwitchPortData> switchPortDataList=new ArrayList<>();
        SwitchPortData switchPortData;
        String result=telnetUtils.sendCommand(command);
        String[] arg = result.split("\n");
        int remarkD = 0;
        int remarkSpace=0;
        String name="";
        String input="";;
        String output="";;
        StringBuilder stringBuilder=new StringBuilder();
        for (int i = 0; i < arg.length; i++) {
            if (!arg[i].equals("") && (arg[i].startsWith("10GE") || arg[i].startsWith("40GE")) && arg[i].contains("current state :")) {
                remarkD = 1;
            }
            if (arg[i].trim().equals("") && stringBuilder.length() > 0 && remarkSpace == 3) {
                switchPortData = new SwitchPortData();
                switchPortData.setSwitchName(cabSwitch.getName());
                switchPortData.setPortName(name);
                switchPortData.setIp(cabSwitch.getAddress());
                if (beforeSwitchPortDataMap.containsKey(cabSwitch.getName() + ":" + name)) {
                    SwitchPortData before = beforeSwitchPortDataMap.get(cabSwitch.getName() + ":" + name);
                    switchPortData.setOutput((Double.parseDouble(output) - before.getOutputTotal()) / 15 * 60 / 1024 / 1024);
                    switchPortData.setInput((Double.parseDouble(input) - before.getInputTotal()) / 15 * 60 / 1024 / 1024);
                } else {
                    switchPortData.setInput(Double.parseDouble(input));
                    switchPortData.setOutput(Double.parseDouble(output));
                }
                switchPortData.setInputTotal(Double.parseDouble(input));
                switchPortData.setOutputTotal(Double.parseDouble(output));
                switchPortData.setProgramTime(programDate);
                switchPortData.setTxt(stringBuilder.toString());
                switchPortDataList.add(switchPortData);
                stringBuilder.setLength(0);
                output = "";
                remarkD = 0;
                remarkSpace = 0;
            }
            if (remarkD != 0) {
                if (remarkD == 1) {
                    name = arg[i].substring(0, 10).trim();
                }
                if (arg[i].contains("Input :")) {
                    input = arg[i].substring(arg[i].indexOf(":") + 1, arg[i].indexOf("bytes")).trim();
                }
                if (arg[i].contains("Output:") && output.equals("")) {
                    output = arg[i].substring(arg[i].indexOf(":") + 1, arg[i].indexOf("bytes")).trim();
                }
                stringBuilder.append(arg[i] + "\n");
                remarkD++;
                if (arg[i].trim().equals("")) {
                    remarkSpace++;
                }
            }
        }
        return switchPortDataList;
    }


    @Override
    public void exitEquipment(TelnetUtils telnetUtils, String command) {
        telnetUtils.sendCommand(command);
    }
}
